/*
 * =====================================================================================
 *
 *       Filename:  test__signalhandler.cxx
 *
 *    Description:  
 *
 *        Version:  1.0
 *        Created:  05/06/2011 10:06:10 PM
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  Chen Ken (Ken), zlchen.ken@gmail.com
 *        Company:  EMC
 *
 * =====================================================================================
 */

#include <sys/signalfd.h>
#include <signal.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>

#define handle_error(msg) \
	do { perror(msg); exit(EXIT_FAILURE);}while(0)

int main(int argc, char* argv[])
{
	sigset_t mask;
	int sfd;
	struct signalfd_siginfo fdsi;
	ssize_t s;

	sigemptyset(&mask);
	sigaddset(&mask, SIGINT);
	sigaddset(&mask, SIGQUIT);

	if(sigprocmask(SIG_BLOCK, &mask, NULL) < 0)
	{
		handle_error("sigprocmask");
	}

	sfd = signalfd(-1, &mask, 0);
	if(sfd < 0)
	{
		handle_error("signalfd");
	}

	for(;;)
	{
		s = read(sfd, &fdsi, sizeof(fdsi));
		if(s != sizeof(fdsi))
		{
			handle_error("read");
		}

		if(fdsi.ssi_signo == SIGINT)
		{
			printf("Got SIGINT\n");
		}
		else if(fdsi.ssi_signo == SIGQUIT)
		{
			printf("Got SIGQUIT\n");
			exit(0);
		}
		else
		{
			printf("Read unexpected signal\n");
		}
	}

	return 0;
}

